package util;

public class MyLinkedList<T> {
	class Node<T extends Object> {
		private Node<T> prev;
		private Node<T> next;
		private T data;
		public Node(T t){
			this.data=t;
		}
		public Node<T> getPrev() {
			return prev;
		}
		public void setPrev(Node<T> prev) {
			this.prev = prev;
		}
		public Node<T> getNext() {
			return next;
		}
		public void setNext(Node<T> next) {
			this.next = next;
		}
		public T getData() {
			return data;
		}
		public void setData(T data) {
			this.data = data;
		}	
	}
	
	private Node<T> head;
	private Node<T> pointer;
	private int length;

	public MyLinkedList() {
		length=0;
		head = new Node<T>(null);
		pointer = head;
	}

	public int getLength() {
		return length;
	}

	public void add(T t) {
		Node<T> n = new Node<T>(t);
		if(head==pointer) {
			head.setNext(n);
			n.setPrev(head);
			pointer=n;
		}
		else{
			n.setPrev(pointer);
			pointer.setNext(n);
			
		}
		pointer=n;
		length++;

	}
	public T get(int index){
		Node<T> node=head;
		if(index<0 || index>=length) throw new ArrayIndexOutOfBoundsException();
		for(int i=0;i<=index;i++){
			node=node.getNext();
		}
		return node.getData();
	}
	
	public static void main(String[] args) {
		MyLinkedList<String> list=new MyLinkedList<String>();
		list.add("abc");
		list.add("bcd");
		for(int i=0;i<list.getLength();i++){
			System.out.println(list.get(i));
		}
		
	}
}
